package com.gzw.shop.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gzw.shop.App;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.bean.User;
import com.gzw.shop.http.OkHttpHelper;
import com.gzw.shop.http.SpotsCallBack;
import com.gzw.shop.msg.LoginRespMsg;
import com.gzw.shop.utils.DESUtil;
import com.gzw.shop.utils.ToastUtils;
import com.gzw.shop.widget.ClearEditText;
import com.gzw.shop.widget.GToolbar;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    GToolbar mToolbar;
    @BindView(R.id.etxt_phone)
    ClearEditText mEtxtPhone;
    @BindView(R.id.etxt_pwd)
    ClearEditText mEtxtPwd;
    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.txt_toReg)
    TextView mTxtToReg;

    private OkHttpHelper mHelper = OkHttpHelper.getInstance();

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {

        mToolbar.setIvLeftOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_OK);
                finish();
            }
        });

    }

    @Override
    public void initData() {

    }

    @OnClick(R.id.txt_toReg)
    public void toRrg() {
        Intent intent = new Intent(this, RegActivity.class);
        startActivity(intent);
    }


    @OnClick(R.id.btn_login)
    public void login() {
        String phone = mEtxtPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = mEtxtPwd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }


        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("password", DESUtil.encode(Contans.DES_KEY, pwd));

        mHelper.post(Contans.API.LOGIN, map, new SpotsCallBack<LoginRespMsg<User>>(this) {

            @Override
            public void onSuccess(Response response, LoginRespMsg<User> userLoginRespMsg) {


                if (userLoginRespMsg.getStatus() == 1) {
                    User user = userLoginRespMsg.getData();
                    String token = userLoginRespMsg.getToken();

                    App app = App.getInstance();
                    app.putUser(user, token);


                    if (app.getIntent() == null) {
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        app.jumpToTargetActivity(LoginActivity.this);
                        finish();
                    }

                } else {
                    ToastUtils.show(LoginActivity.this, userLoginRespMsg.getMessage());
                }
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(RESULT_OK);
            finish();
        }

        return super.onKeyDown(keyCode, event);
    }
}
